package me.techbird.nve.service.impl;

import me.techbird.nve.dao.QuestionDao;
import me.techbird.nve.dao.ReportDao;
import me.techbird.nve.dao.UserDao;
import me.techbird.nve.dao.impl.QuestionDaoImpl;
import me.techbird.nve.dao.impl.ReportDaoImpl;
import me.techbird.nve.dao.impl.UserDaoImpl;
import me.techbird.nve.domain.*;
import me.techbird.nve.service.UserService;
import java.util.List;

public  class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    private ReportDao reportDao = new ReportDaoImpl();
    private QuestionDao questionDao = new QuestionDaoImpl();

    @Override
    public User login(User user) {
        return userDao.findByStuNoAndPassword(user.getStu_no(),user.getPassword());
    }

    @Override
    public PageBean<User> pageQuery(int currentPage, int pageSize) {
        //封装PageBean
        PageBean pageBean = new PageBean();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);

        int totalCount = userDao.findTotalCount();
        System.out.println("查询到---"+totalCount+"条用户信息");

        pageBean.setTotalCount(totalCount);

        int start = (currentPage - 1) * pageSize;
        List<User> users = userDao.findByPage(start, pageSize);
        pageBean.setList(users);

        System.out.println(users);

        int totalPage = totalCount % pageSize == 0 ? totalCount/pageSize  : (totalCount / pageSize) + 1;
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }

    @Override
    public boolean removeUser(User user) {
        int status = userDao.removeUserByUid(user.getUid());
        return status != 0;
    }

    @Override
    public boolean addUser(User user) {
        int status = userDao.add(user);
        return status != 0;
    }

    @Override
    public boolean updateUser(User user) {
        int status = userDao.updateUser(user);
        return status != 0;
    }

    @Override
    public List<User> findAllUser() {
        return userDao.findAllUser();
    }

    @Override
    public User findByUid(String uid) {
        return userDao.findByUid(uid);
    }

    @Override
    public boolean addReport(ExpReport report) {
        int status = reportDao.addReport(report);
        return status != 0;
    }

    @Override
    public List<ExamQuestion> findQuestions() {
        List<ExamQuestion> questions = questionDao.findQuestion();
        for (ExamQuestion qs : questions) {
            System.out.println("----oid:"+qs.getOid());
            ExamOptions option = questionDao.findOption(qs.getOid());
            qs.setOptions(option);
        }
        return questions;
    }

    @Override
    public boolean addTestResult(ExamTest test) {
        int status = questionDao.addTest(test);
        return status != 0;
    }

    @Override
    public PageBean<ExpReport> findAllReport(int currentPage, int pageSize) {
        PageBean bean = new PageBean();
        bean.setCurrentPage(currentPage);
        bean.setPageSize(pageSize);
        int reportCount = reportDao.findReportCount();

        bean.setTotalCount(reportCount);
        System.out.println("----查询到报告信息："+reportCount+"条");

        int start = (currentPage-1) * pageSize;
        List<ExpReport> list = reportDao.findByPage(start, pageSize);

        for (ExpReport report : list){
            User user = userDao.findByUid(report.getUid());
            report.setUser(user);
        }

        bean.setList(list);

        int totalPage = reportCount%pageSize == 0 ? reportCount/pageSize : (reportCount/pageSize)+1;
        bean.setTotalPage(totalPage);
        return bean;
    }

    @Override
    public PageBean<ExamTest> findAllTest(int currentPage, int pageSize) {

        PageBean bean = new PageBean();
        bean.setCurrentPage(currentPage);
        bean.setPageSize(pageSize);
        int testCount = reportDao.findTestCount();

        bean.setTotalCount(testCount);
        System.out.println("----查询到测试信息："+testCount+"条");

        int start = (currentPage-1) * pageSize;
        List<ExamTest> list = reportDao.findTestByPage(start, pageSize);

        for (ExamTest test : list){
            User user = userDao.findByUid(String.valueOf(test.getUid()));
            test.setUser(user);
        }

        bean.setList(list);

        int totalPage = testCount%pageSize == 0 ? testCount/pageSize : (testCount/pageSize)+1;
        bean.setTotalPage(totalPage);
        return bean;
    }


}
